package com.willren;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
	
public class SettingActivity extends Activity {

	private boolean isLandscape;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		super.onCreate(savedInstanceState);

		/**
		 * Get the orientation of screen when the activity was start
		 */
		Display display = getWindowManager().getDefaultDisplay();
		isLandscape = display.getWidth() > display.getHeight();

		/**
		 * Initialize the layout of activity by child class If the activity was
		 * killed just now, savedInstanceState is not null
		 */
		onInitLayout(isLandscape, savedInstanceState);
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);

		/**
		 * Adjust the layout by child class for each orientation
		 */
		if (Configuration.ORIENTATION_LANDSCAPE == newConfig.orientation) {
			if (false == isLandscape) {
				isLandscape = true;
				onScreenOrientationChanged(true);
			}
		} else if (Configuration.ORIENTATION_PORTRAIT == newConfig.orientation) {
			if (true == isLandscape) {
				isLandscape = false;
				onScreenOrientationChanged(false);
			}
		}
	}

	/**
	 * Launched when the activity was build
	 */
	public void onInitLayout(boolean isLandscape, Bundle data) {
		if (isLandscape) {
			setContentView(R.layout.setting_l);
		} else {
			setContentView(R.layout.setting_p);
		}
		
		resetData();
	}

	/**
	 * Launched when the orientation of screen was switch
	 */
	public void onScreenOrientationChanged(boolean isLandscape) {
		if (isLandscape) {
			setContentView(R.layout.setting_l);
		} else {
			setContentView(R.layout.setting_p);
		}
		
		resetData();
	}

	/**
	 * Set the data of View
	 */
	public void resetData() {
		Button setting_button_close = (Button) findViewById(R.id.setting_button_close);
		if (null != setting_button_close) {
			setting_button_close.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					finish();
				}
			});
		}
	}
}